package com.kaver.sso.dao.mapper;

import com.kaver.sso.dao.po.SsoRights;
import com.kaver.sso.dao.po.SsoRoleRights;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * <p>
 * 角色权限表 Mapper 接口
 * </p>
 *
 * @author roc
 * @since 2021-08-06
 */
public interface SsoRoleRightsMapper extends BaseMapper<SsoRoleRights> {
    
    @Select("<script>SELECT DISTINCT A.* FROM sso_rights A INNER JOIN sso_role_rights B ON A.id = B.rights_id INNER JOIN sso_role C ON C.id = B.role_id WHERE A.deleted = 0 AND B.deleted = 0 AND C.deleted = 0 AND C.id IN <foreach collection=\"roleIds\" index = \"index\" item = \"id\" \r\n" + 
        "            open= \"(\" separator=\",\" close=\")\"> \r\n" + 
        "                #{id} \r\n" + 
        "            </foreach></script>")
    List<SsoRights> queryRoleRights(@Param("roleIds") List<Long> roleIds);
    
    @Select("SELECT DISTINCT A.* FROM sso_rights A INNER JOIN sso_role_rights B ON A.id = B.rights_id INNER JOIN sso_role C ON C.id = B.role_id INNER JOIN sso_user_role D ON D.role_id = C.id WHERE A.deleted = 0 AND B.deleted = 0 AND C.deleted = 0 AND D.deleted = 0 AND D.user_id = #{userId} AND A.group_id = #{groupId}")
    List<SsoRights> queryUserRights(@Param("userId")String userId, @Param("groupId")Long groupId);
    
    @Select("SELECT count(1) FROM sso_rights A INNER JOIN sso_role_rights B ON A.id = B.rights_id INNER JOIN sso_role C ON C.id = B.role_id INNER JOIN sso_user_role D ON D.role_id = C.id WHERE A.deleted = 0 AND B.deleted = 0 AND C.deleted = 0 AND D.deleted = 0 AND D.user_id = #{userId} AND A.group_id = #{groupId} AND A.right_identifier = #{rightIdentifier}")
    int queryUserHaveRight(@Param("userId")String userId, @Param("groupId")Long groupId, @Param("rightIdentifier")String rightIdentifier);
}
